#include "draw.h"

   void drawImage(SDL_Surface *image, int x, int y)
  {
       SDL_Rect dest;

       

       dest.x = x;
       dest.y = y;
       dest.w = image->w;
       dest.h = image->h;
       
      

       SDL_BlitSurface(image, NULL, jeu.ecran, &dest);
   }
    void drawLifeJauge(SDL_Surface *image, int x, int y, int imgW)
  {
       SDL_Rect dest;

       

       dest.x = x;
       dest.y = y;
       SDL_Rect src;

   
    src.x=0;
    src.y=0;
    src.w = imgW;
    src.h = 50;
       
      

       SDL_BlitSurface(image, &src, jeu.ecran, &dest);
   }

     void drawHud(int maxW)
    {
    
    char text[200];


    drawImage(jeu.HUD_vie, 100, 20); 
    sprintf(text, "%d", jeu.vies);
    drawString(text, 170, 30, font);

    
    drawString("Score: ", (maxW - 200),20,font);
    sprintf(text, "%d", jeu.etoiles);
    drawString(text, (maxW - 100), 20, font);
  
    }



  void draw(int maxW, int maxH)
  {

       int i,j;
       
       
      drawImage(map.background, 0, 0);

      
      drawMap(maxW,maxH);

      
      drawHud(maxW);
       for(i = 0 ; i < jeu.nombreMonstres ; i++)
        {
           drawAnimatedEntity(&monster[i]);

        }
        for(j = 0 ; j < jeu.nombreButin ; j++)
        {
           drawAnimatedButin(&Butin[j]);

        }
        drawanimatedplayer();

        
    
   
    
    SDL_Flip(jeu.ecran);
    

    
    

  }

   void drawTile(SDL_Surface *image, int destx, int desty, int srcx, int srcy)
  {
    
    SDL_Rect dest;

    dest.x = destx;
    dest.y = desty;
    dest.w = TILE_SIZE;
    dest.h = TILE_SIZE;

    
    SDL_Rect src;

    src.x = srcx;
    src.y = srcy;
    src.w = TILE_SIZE;
    src.h = TILE_SIZE;

    

    SDL_BlitSurface(image, &src, jeu.ecran, &dest);

  }

   SDL_Surface *loadImage(char *name)
   {
       

       SDL_Surface *temp = IMG_Load(name);
       SDL_Surface *image;

       
      if (temp == NULL)
      {
           printf("Failed to load image %s\n", name);

          return NULL;
       }

       

       SDL_SetColorKey(temp, (SDL_SRCCOLORKEY | SDL_RLEACCEL), SDL_MapRGB(temp->format, TRANS_R, TRANS_G,    TRANS_B));


       

       image = SDL_DisplayFormat(temp);
       
       
       SDL_FreeSurface(temp);

       if (image == NULL)
       {
           printf("Failed to convert image %s to native format\n", name);

           return NULL;
       }

       

       return image;


   }



  void delay(unsigned int frameLimit)
  {

    

    unsigned int ticks = SDL_GetTicks();

    if (frameLimit < ticks)
    {
        return;
    }

    if (frameLimit > ticks + 16)
    {
        SDL_Delay(16);
    }

    else
    {
        SDL_Delay(frameLimit - ticks);
    }
  }




   void drawBackground(SDL_Surface *image, int x, int y,int maxW, int maxH)
   {
      SDL_Surface *Debut = NULL , *Final = NULL;

      double SX,SY,destx,desty;
      SX= image->w;
      SY= image->h;
      SDL_Rect dest;

      destx = maxW/SX;
      desty = maxH/SY;
      dest.x=0;
      dest.y=0;


      Final = rotozoomSurfaceXY(image,0.0, destx,desty,0);



      SDL_BlitSurface(Final, NULL, jeu.ecran, &dest);
      SDL_FreeSurface(Final);
   }

     



   void drawmap2(int maxW, int maxH,  int imgW)
  {

       int i,j,retour,retour2;

       static int etat=0, imgWidthEnnemy=180, collFire=0, regeneration=0,mort=0, repeter=0; // 1-> tir a droite 2-> tir a gauche
       
       
      drawBackground(map.backgroundStage2,0,0,maxW,maxH);
      drawJauje(maxW,imgW, imgWidthEnnemy);
      lvl2(maxW, maxH, &shuriken,&Ninja,collFire,mort);
      drawanimatedplayer2();

       if(input.attack==1 && player.direction == RIGHT)
        {
          etat=1;

        
        }
        else if (input.attack == 1 && player.direction == LEFT)
        {
          etat=2;
        }
       

        if (etat==1)
        {
          retour = attackAnimRight(&feu,&player, maxW, maxH, collFire,&Ninja);
          
         
        }
        if (etat == 2)
        {
          retour2 = attackAnimLeft(&feuG,&player, maxW, maxH);
          
        }




        if (retour == 0|| retour2 == 0)
        {
          etat=0;


        }

        collFire=collideFire(&feu, &Ninja);

       if (collFire==1)
       {
          imgWidthEnnemy-= 1;
          if (imgWidthEnnemy<80 && regeneration ==0 )
          {
            imgWidthEnnemy=150;
            regeneration=1;
          }
          if (imgWidthEnnemy < 0)
          {
              mort=1;
              EnnemyDeath=1;
              if (repeter ==0)
              {
                  Mix_PauseMusic();
                  playSoundFx(WIN);
                  
                  

                  repeter=1;

              }
              
          }
       }
}

       void drawmap3(int maxW, int maxH)
       {
        drawBackground(map.backgroundStage3,0,0,maxW,maxH);
        drawBoutons( &boutons, maxW,  maxH);

       }


        


        
    
   
    
    


    

    
    

  


